const url = require('url');
const got = require('@/utils/got');
const cheerio = require('cheerio');

module.exports = async (ctx) => {
    let items = [];

    const url_codemart = 'https://codemart.com/api/project?';
    const resp_codemart = await got({
        method: 'get',
        url: url_codemart,
        headers: {
            'Accept': 'application/json'
        }
    });
    const json_codemart = resp_codemart.data.rewards;
    for(let i=0; i<json_codemart.length; i++) {
        const it = json_codemart[i];
        items.push({
            title: it.name,
            link: `https://codemart.com/project/${it.id}`,
            pubDate: new Date(it.pubTime).toUTCString(),
            description: JSON.stringify(it),
        });
    }

    const url_oschina = 'https://zb.oschina.net/project/contractor-browse-project-and-reward?type=1';
    const resp_oschina = await got({
        method: 'get',
        url: url_oschina,
        headers: {
            'Accept': 'application/json'
        }
    });
    const json_oschina = resp_oschina.data.data.data;
    for(let i=0; i<json_oschina.length; i++) {
        const it = json_oschina[i];
        items.push({
            title: it.name,
            link: `https://zb.oschina.net/project/detail.html?id=${it.id}`,
            pubDate: new Date(it.publishTime + ' GMT+8').toUTCString(),
            description: JSON.stringify(it),
        });
    }

    ctx.state.data = {
        title: '码市/oschina',
        link: 'https://mhugh.net',
        item: items,
    };
};
